<?php
/**
 * Created by PhpStorm.
 * Author: sitenv@aliyun.com
 * CreateTime: 2021/07/11 17:54
 * Blog：www.myblogs.xyz
 */

namespace app\admin\controller;


use think\facade\View;

class Setting extends Common
{
    // 站点设置
    public function site()
    {
        $this->saveSetting('site');
        return View::fetch();
    }

    // 基本设置
    public function basic()
    {
        $this->saveSetting('basic');
        return View::fetch();
    }

    // 其它设置
    public function other()
    {
        $this->saveSetting('other');
        return View::fetch();
    }

    // 支付宝APP支付设置
    public function aliPay()
    {
        $this->saveSetting('ali_pay');
        return View::fetch();
    }

    // 微信公众号支付设置
    public function wxPublic()
    {
        $this->saveSetting('wx_public');
        return View::fetch();
    }

    // 微信APP支付设置
    public function wxPay()
    {
        $this->saveSetting('wx_pay');
        return View::fetch();
    }

    // 微信小程序设置
    public function wxApplet()
    {
        $this->saveSetting('wx_applet');
        return View::fetch();
    }

    // 短信设置
    public function sms()
    {
        $this->saveSetting('sms');
        return View::fetch();
    }

    // 邮箱设置
    public function email()
    {
        $this->saveSetting('email');
        return View::fetch();
    }

    // 地图设置
    public function maps()
    {
        $this->saveSetting('maps');
        return View::fetch();
    }

    // 费率设置
    public function rate()
    {
        $this->saveSetting('rate');
        return View::fetch();
    }

    protected function saveSetting($type = '')
    {
        if (!empty($type)){
            if (request()->isPost())
            {
                $data = input('post.');
                $setting_model = new \app\common\model\Setting();
                if (!$setting_model->insetSetting($type, $data)){
                    $this->returnError('保存失败');
                }
                $this->returnSuccess('保存成功');
            }
            $setting_data = [];
            $field_path = APP_PATH . 'setting/' . $type . '.json';
            if (file_exists($field_path)){
                $json_data = file_get_contents($field_path);
                $oldData = json_decode($json_data, true);
                $setting_data = $oldData[$type];
            }
            View::assign($type, $setting_data);
        }
    }
}